Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
How store address of an array[] in a variable

This seems like a silly question. I have an array of chars and want to store the address of the array in another variable, but can't seem to declare the correct type for the array address (I'm using gcc):


IN:

int main(void){
  char cha[] = "abcde";
  char **arrayAddress = &cha;
}

OUT:

arrayaddress.c: In function ‘main’:
arrayaddress.c:3:25: warning: initialization of ‘char **’ from incompatible pointer type ‘char (*)[6]’ [-Wincompatible-pointer-types]
    3 |   char **arrayAddress = &cha;
      |                         ^

This is expected, I have read elsewhere that the type of cha should be char(*)[6]. But when I try to declare arrayAddress with this type, my program fails:


IN:

int main(void){
  char cha[] = "abcde";
  char (*)[6]arrayAddress = &cha;
}

OUT:

arrayaddress.c: In function ‘main’:
arrayaddress.c:3:10: error: expected identifier or ‘(’ before ‘)’ token
    3 |   char (*)[6]arrayAddress = &cha;
      |          ^
make: *** [<builtin>: arrayaddress] Error 1
       ^

How do I define arrayAddress correctly?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

It is written:

char (*arrayAddress)[6] = &cha;

Notice that the name of variable gets tucked in middle of the expression.

over 4 years ago · Santiago Trujillo Report

0

Arrays decay to pointers.

  char cha[] = "abcde";

  char *p1 = cha;

  char (*arrayptr)[sizeof(cha)] = &cha;

cha, &cha[0] and &cha reference the same first element of the array cha, the only difference is the type.

  1. cha and &cha[0] has type: pointer to char
  2. &cha has type: pointer to array of 6 char elements.
over 4 years ago · Santiago Trujillo Report

0

If your compiler supports typeof extension (gcc does) then you can define the pointer as:

typeof(char (*)[6]) arrayAddress = &cha;

Or even cleaner as:

typeof(char[6]) * arrayAddress = &cha;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!